package demo1;

public class demo_06isDaemon {
    //演示后台线程与前台进程的区别
    public static void main(String[] args) {
        //创建thread1线程，没有设置前后台线程默认为前台线程
        Thread thread1 =new Thread(()->{
            //线程任务并不是死循环执行5次即可完成
            for (int i = 0; i <5 ; i++) {
                System.out.println("线程thread1正在运行");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //创建thread2为后台线程，需要在线程启动之前设置
        Thread thread2 =new Thread(()->{
            //线程任务是死循环打印
            while (true){
                System.out.println("线程thread2正在运行");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //设置为后台线程
        thread2.setDaemon(true);
        //启动线程
        thread1.start();
        thread2.start();
    }
}
